using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;

public class FileReader : MonoBehaviour {

    public string m_FileName;

    private Dictionary<string, string> m_Dictionary;

    private List<string> m_KeyWords;

    private List<int> m_UsedKeyWords;

	// Use this for initialization
	void Start () {
        m_Dictionary = new Dictionary<string,string>();
        m_KeyWords = new List<string>();
        m_UsedKeyWords = new List<int>();

	    StreamReader sr = new StreamReader("./" + m_FileName);
        string fileContents = sr.ReadToEnd();
        sr.Close();

        string[] lines = fileContents.Split("\n"[0]);
        foreach(string s in lines)
        {
            if (!s.StartsWith("#"))
            {
                s.Replace(" =", "=");
                s.Replace("= ", "=");
                int index = s.IndexOf("=");
                if (index > 0)
                {
                    string key = s.Substring(0, index);
                    m_Dictionary.Add(key, s.Substring(index));
                    m_KeyWords.Add(key);
                }
            }
        }
	}

    public string GetRandomKeyWord()
    {
        int generated = 0;
        int index;
        do
        {
            index = UnityEngine.Random.Range(0, m_KeyWords.Count - 1);
        }
        while (++generated < 10 && m_UsedKeyWords.Contains(index));

        m_UsedKeyWords.Add(index);

        return m_KeyWords[index];
    }

    public string GetCorrectAnswer(string key)
    {
        string output = "";

        if (m_Dictionary.ContainsKey(key))
        {
            m_Dictionary.TryGetValue(key, out output);
        }

        return output;
    }
}